import {
  Controller,
  Body,
  Post,
  HttpCode,
  HttpStatus,
  Request,
  Get,
  Query,
} from '@nestjs/common'
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'
import { AuthService } from './auth.service'
import { Public } from './decorators/public.decorators'
import { UserClass } from '../user/user.model'

@ApiBearerAuth()
@ApiTags('登入身份认证')
@Controller('auth')
export class AuthController {
  constructor(private authService: AuthService) {}

  @Public()
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '身份认证登入', description: '获取token' })
  @Post('login')
  public signIn(@Body() sigInDto: UserClass) {
    return this.authService.signIn(sigInDto.username, sigInDto.password)
  }

  @Get('profile')
  @ApiOperation({ summary: '获取概要', description: '通过token获取' })
  getProfile(@Request() req: any) {
    return req.user // 返回用户信息
  }

  @Public()
  @Get('refresh')
  @ApiOperation({ summary: '刷新token', description: '通过refresh_token获取' })
  refreshToken(@Query('token') token: string) {
    return this.authService.refreshToken(token)
  }
}
